package com.aaa.car.controller;

import com.aaa.car.entity.MaintainRecord;
import com.aaa.car.entity.ViolationRecord;
import com.aaa.car.service.ViolationRecordService;
import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 违章记录表(ViolationRecord)表控制层
 *
 * @author makejava
 * @since 2020-11-23 19:06:48
 */
@RestController
@RequestMapping("violationRecord")
public class ViolationRecordController {
    /**
     * 服务对象
     */
    @Resource
    private ViolationRecordService violationRecordService;

    /**
     * 查询
     * @param pageNo
     * @param pageSize
     * @param violationRecord
     * @return
     */
    @GetMapping("queryViolationRecord")
    public Result queryViolationRecord(Integer pageNo, Integer pageSize,ViolationRecord violationRecord) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return violationRecordService.queryViolationRecord(pageNo,pageSize,violationRecord);
    }

    /**
     * 添加
     * @param violationRecord
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody ViolationRecord violationRecord){
        //判断新增的角色名字为空抛出异常
        if (violationRecord.getCarNo()==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }else{
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.violationRecordService.insert(violationRecord));
        }
    }

    /**
     * 修改
     * @param violationRecord
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody ViolationRecord violationRecord){
        //当ID空为时抛出自定义异常
        if (violationRecord.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.violationRecordService.update(violationRecord));
    }

    /**
     * 删除
     * @param Id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer Id){
        //当ID空为时抛出自定义异常
        if (null == Id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.violationRecordService.deleteById(Id));
    }

}